using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Forms;
namespace Easislides
{
	public class FrmLaunchMediaPlayer : Form
	{
		public delegate void Message(int MsgCode, string MsgString);
		private IContainer components = null;
		private System.Windows.Forms.Timer TimerAttemptConnect;
		private System.Windows.Forms.Timer TimerRefresh;
		private bool FormFirstLoad = true;
		private int AttemptConnectCount = 0;
		private int MaxAttemptConnectCount = 60;
		private string PreviousItemLocation = "";
		private bool ItemFirstPlay = false;
		private Cursor LiveCursor;
		private Thread timerThread;
		private DShowLib DShowPlayer = new DShowLib();
		public event FrmLaunchMediaPlayer.Message OnMessage
		{
			[MethodImpl(MethodImplOptions.Synchronized)]
			add
			{
                //this.OnMessage = (FrmLaunchMediaPlayer.Message)Delegate.Combine(this.OnMessage, value);
			}
			[MethodImpl(MethodImplOptions.Synchronized)]
			remove
			{
                //this.OnMessage = (FrmLaunchMediaPlayer.Message)Delegate.Remove(this.OnMessage, value);
			}
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.TimerAttemptConnect = new System.Windows.Forms.Timer(this.components);
            this.TimerRefresh = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // TimerRefresh
            // 
            this.TimerRefresh.Enabled = true;
            this.TimerRefresh.Interval = 500;
            this.TimerRefresh.Tick += new System.EventHandler(this.TimerRefresh_Tick);
            // 
            // FrmLaunchMediaPlayer
            // 
            this.BackColor = System.Drawing.Color.Black;
            this.ClientSize = new System.Drawing.Size(125, 110);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "FrmLaunchMediaPlayer";
            this.ShowInTaskbar = false;
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmLaunchMediaPlayer_FormClosing);
            this.Load += new System.EventHandler(this.FrmLaunchMediaPlayer_Load);
            this.VisibleChanged += new System.EventHandler(this.FrmLaunchMediaPlayer_VisibleChanged);
            this.ResumeLayout(false);

		}
		public FrmLaunchMediaPlayer()
		{
			this.InitializeComponent();
		}
		private void FrmLaunchMediaPlayer_Load(object sender, EventArgs e)
		{
			Bitmap bitmap = new Bitmap(55, 25);
			Graphics graphics = Graphics.FromImage(bitmap);
			IntPtr hicon = bitmap.GetHicon();
			this.LiveCursor = new Cursor(hicon);
			this.InitForm();
		}
		private void InitForm()
		{
			this.SetShowWindow();
			this.BackColor = Color.Black;
			if (gf.WMP_Present)
			{
				this.InitMediaPlayer();
				this.timerThread = new Thread(new ThreadStart(this.ThreadProc));
				this.timerThread.IsBackground = true;
				this.timerThread.Start();
			}
			this.Remote_SendScreenToBack();
		}
		private void InitMediaPlayer()
		{
			if (gf.WMP_Present)
			{
				this.DShowPlayer.Parent = this;
				this.DShowPlayer.Location = new Point(0, 0);
				this.DShowPlayer.Dock = DockStyle.None;
				int num = gf.LS_Width * gf.VideoSize / 100;
				int num2 = gf.LS_Height * gf.VideoSize / 100;
				int num3 = (gf.LS_Width - num) / 2 - 1;
				num3 = ((num3 < 0) ? 0 : num3);
				int t = (gf.VideoVAlign == 1) ? ((gf.LS_Height - num2) / 2) : ((gf.VideoVAlign == 2) ? (gf.LS_Height - num2) : 0);
				this.DShowPlayer.SetDefaultSize(num3, t, num, num2, (gf.VAlign)gf.VideoVAlign);
				this.DShowPlayer.ForeColorChanged += new EventHandler(this.DShowPlayer_ForeColorChanged);
			}
		}
		private void DShowPlayer_ForeColorChanged(object sender, EventArgs e)
		{
			if (this.DShowPlayer.ForeColor == Color.Red)
			{
				if (gf.ShowRunning)
				{
					if (this.DShowPlayer.MouseBtnPressed == MouseButtons.Left)
					{
                        //this.OnMessage(11, "");
					}
					else
					{
						if (this.DShowPlayer.MouseBtnPressed == MouseButtons.Right)
						{
                            //this.OnMessage(12, "");
						}
					}
				}
			}
			else
			{
				if (this.DShowPlayer.ForeColor == Color.Blue)
				{
					if (gf.ShowRunning)
					{
                        //this.OnMessage(13, "");
					}
				}
				else
				{
					switch (this.DShowPlayer.currentState)
					{
					case gf.PlayState.Stopped:
						this.DShowPlayer.currentVolume = 0;
						if (!gf.ShowLiveCam)
						{
							gf.MediaPlayedLapseTime = DateTime.Now.Subtract(gf.MediaLiveItemStartTime) + new TimeSpan(0, 0, 1);
						}
						break;
					case gf.PlayState.Running:
					{
						this.TimerAttemptConnect.Stop();
						gf.MediaNotifyItemConnecting = false;
						if (this.ItemFirstPlay)
						{
							if (!gf.ShowLiveCam)
							{
								gf.MediaLiveItemStartTime = DateTime.Now;
								gf.MediaPlayedLapseTime = new TimeSpan(0L);
							}
						}
						else
						{
							gf.MediaDoRotate = false;
						}
						this.ItemFirstPlay = false;
						bool loopClip = false;
						int clipDuration = this.DShowPlayer.GetClipDuration();
						if (clipDuration == gf.LiveItem.RotateTotal || (gf.LiveItem.RotateStyle == 2 && gf.LiveItem.RotateTotal == 0))
						{
							if (!gf.ShowLiveCam)
							{
								gf.MediaLengthAsRotateLength = true;
							}
						}
						else
						{
							if (!gf.ShowLiveCam)
							{
								gf.MediaLengthAsRotateLength = false;
							}
							if (gf.LiveItem.Format.MediaRepeat > 0 && !gf.RestartCurrentItem)
							{
								loopClip = true;
							}
						}
						this.DShowPlayer.LoopClip = loopClip;
						break;
					}
					}
				}
			}
		}
		public void Remote_StopItem()
		{
			if (gf.WMP_Present)
			{
				this.DShowPlayer.CloseClip();
			}
		}
		public void Remote_PauseItem()
		{
			if (gf.WMP_Present)
			{
				this.DShowPlayer.PauseClip();
			}
		}
		public void Remote_PausePlayItem()
		{
			if (gf.WMP_Present)
			{
				this.DShowPlayer.PausePlayClip();
			}
		}
		public int Remote_LoadItem()
		{
			int result;
			if (gf.WMP_Present)
			{
				result = this.LoadMedia(false, true);
			}
			else
			{
				result = 0;
			}
			return result;
		}
		public int Remote_ResumeItem()
		{
			int result;
			if (gf.WMP_Present)
			{
				result = this.LoadMedia(true, false);
			}
			else
			{
				result = 0;
			}
			return result;
		}
		public void Remote_ResumeItemFromStart()
		{
			if (gf.WMP_Present)
			{
				this.DShowPlayer.ResumeFromStart();
			}
		}
		public int Remote_LoadLiveCam()
		{
			int result;
			if (gf.WMP_Present)
			{
				result = this.LoadLiveCam();
			}
			else
			{
				result = 0;
			}
			return result;
		}
		public int Remote_UpdateLiveCam()
		{
			int result;
			if (gf.WMP_Present)
			{
				result = this.UpdateLiveCam();
			}
			else
			{
				result = 0;
			}
			return result;
		}
		public void Remote_ClearScreen()
		{
			if (gf.WMP_Present)
			{
				this.DShowPlayer.CloseClip();
			}
		}
		public int Remote_RepeatItem()
		{
			int result;
			if (gf.WMP_Present)
			{
				gf.MediaNotifyRepeatItem = false;
				result = this.LoadMedia(false, false);
			}
			else
			{
				result = 0;
			}
			return result;
		}
		public void Remote_StopShow()
		{
			this.Remote_StopItem();
			base.Left = 100;
			base.Top = 200;
			base.Hide();
		}
		public bool Remote_ItemPlayingStatus()
		{
			bool result;
			if (gf.WMP_Present)
			{
				if (this.DShowPlayer.currentState == gf.PlayState.Running)
				{
					result = true;
					return result;
				}
			}
			result = false;
			return result;
		}
		public void Remote_RefreshMediaWindow()
		{
			if (gf.WMP_Present)
			{
				try
				{
					int left = this.DShowPlayer.Left;
					int top = this.DShowPlayer.Top;
					int width = this.DShowPlayer.Width;
					int height = this.DShowPlayer.Height;
					this.DShowPlayer.Width = 0;
					this.DShowPlayer.Height = 0;
					this.DShowPlayer.Width = width;
					this.DShowPlayer.Height = height;
					this.DShowPlayer.Invalidate();
				}
				catch
				{
				}
			}
		}
		public void Remote_SendScreenToBack()
		{
			base.SendToBack();
		}
		public bool MediaIsVideo()
		{
			return gf.WMP_Present && !this.DShowPlayer.isVideo;
		}
		public void ThreadProc()
		{
			try
			{
				MethodInvoker method = new MethodInvoker(this.UpdateProgress);
				while (true)
				{
					base.BeginInvoke(method);
					Thread.Sleep(750);
				}
			}
			catch (ThreadInterruptedException var_1_2A)
			{
			}
			catch (Exception var_2_2F)
			{
			}
		}
		private void UpdateProgress()
		{
			if (this.DShowPlayer.currentState == gf.PlayState.Running && !gf.ShowLiveCam)
			{
				int currentPosition = this.DShowPlayer.GetCurrentPosition();
				if (currentPosition >= 0)
				{
					if ((int)gf.MediaPlayedLapseTime.TotalSeconds <= currentPosition)
					{
						if (!gf.ShowLiveCam && gf.MediaDoRotate)
						{
							gf.MediaPlayedLapseTime = new TimeSpan(0, 0, currentPosition);
						}
					}
					else
					{
						if (!gf.ShowLiveCam && gf.MediaDoRotate)
						{
							gf.MediaPlayedLapseTime = DateTime.Now.Subtract(gf.MediaLiveItemStartTime);
						}
					}
				}
			}
		}
		private void SetShowWindow()
		{
			base.Left = gf.LS_Left;
			base.Top = gf.LS_Top;
			base.Height = gf.LS_Height;
			base.Width = gf.LS_Width;
		}
		private void FrmLaunchMediaPlayer_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.DShowPlayer.TidyUp();
		}
		private int LoadMedia(bool ResumeFromPreviousPosition, bool FirstLoad)
		{
			string mediaLocation = gf.GetMediaLocation(gf.LiveItem);
			int currentInputDevice = 1;
			int result;
			if (mediaLocation == "")
			{
				this.DShowPlayer.CloseClip();
				result = 0;
			}
			else
			{
				if (gf.Left(mediaLocation, "<<Capture>>".Length) == "<<Capture>>")
				{
					currentInputDevice = gf.StringToInt(gf.Mid(mediaLocation, "<<Capture>>".Length));
				}
				this.DShowPlayer.SetMute(gf.LiveItem.Format.MediaMute > 0);
				this.DShowPlayer.SetWideScreen(gf.LiveItem.Format.MediaWidescreen > 0, false);
				gf.MediaResetStartTime = true;
				this.DShowPlayer.newFilename = mediaLocation;
				this.DShowPlayer.currentInputDevice = currentInputDevice;
				this.PreviousItemLocation = mediaLocation;
				this.ItemFirstPlay = true;
				this.DShowPlayer.ResumeFromPreviousPosition = ResumeFromPreviousPosition;
				this.DShowPlayer.OpenClip();
				this.DShowPlayer.SetVolume(gf.LiveItem.Format.MediaVolume);
				if (this.DShowPlayer.currentState == gf.PlayState.Running)
				{
					int num = this.DShowPlayer.isVideo ? 2 : 1;
					if (FirstLoad)
					{
						this.DShowPlayer.PauseClip();
					}
					result = num;
				}
				else
				{
					result = 0;
				}
			}
			return result;
		}
		private int LoadLiveCam()
		{
			this.DShowPlayer.SetMute(gf.LiveCamMute);
			this.DShowPlayer.SetWideScreen(gf.LiveCamWidescreen, false);
			gf.MediaResetStartTime = true;
			this.DShowPlayer.newFilename = "<<Capture>>" + gf.LiveCamNumber.ToString();
			this.DShowPlayer.currentInputDevice = gf.LiveCamNumber;
			this.PreviousItemLocation = this.DShowPlayer.newFilename;
			this.ItemFirstPlay = true;
			this.DShowPlayer.ResumeFromPreviousPosition = false;
			this.DShowPlayer.OpenClip();
			this.DShowPlayer.SetVolume(gf.LiveCamVolume);
			int result;
			if (this.DShowPlayer.currentState == gf.PlayState.Running)
			{
				result = (this.DShowPlayer.isVideo ? 2 : 1);
			}
			else
			{
				result = 0;
			}
			return result;
		}
		private int UpdateLiveCam()
		{
			this.DShowPlayer.SetMute(gf.LiveCamMute);
			this.DShowPlayer.SetWideScreen(gf.LiveCamWidescreen, true);
			if (this.PreviousItemLocation != "<<Capture>>" + gf.LiveCamNumber.ToString())
			{
				gf.MediaResetStartTime = true;
				this.DShowPlayer.newFilename = "<<Capture>>" + gf.LiveCamNumber.ToString();
				this.DShowPlayer.currentInputDevice = gf.LiveCamNumber;
				this.PreviousItemLocation = this.DShowPlayer.newFilename;
				this.ItemFirstPlay = true;
				this.DShowPlayer.ResumeFromPreviousPosition = false;
				this.DShowPlayer.OpenClip();
				this.DShowPlayer.SetVolume(gf.LiveCamVolume);
			}
			int result;
			if (this.DShowPlayer.currentState == gf.PlayState.Running)
			{
				result = (this.DShowPlayer.isVideo ? 2 : 1);
			}
			else
			{
				result = 0;
			}
			return result;
		}
		private void FrmLaunchMediaPlayer_VisibleChanged(object sender, EventArgs e)
		{
			if (base.Visible)
			{
				if (this.FormFirstLoad)
				{
					this.FormFirstLoad = false;
				}
				else
				{
					this.InitForm();
				}
			}
		}
		private void TimerRefresh_Tick(object sender, EventArgs e)
		{
		}
		public bool Remote_GetMediaTimings(ref string Duration, ref string Position, ref int intDuration, ref int intPosition)
		{
			bool result;
			if (gf.WMP_Present)
			{
				if (this.DShowPlayer.newFilename != "")
				{
					Duration = ((this.DShowPlayer.newFilename != "") ? this.DShowPlayer.GetClipDurationString() : "00:00");
					Position = this.DShowPlayer.GetCurrentPositionString();
                    intDuration = this.DShowPlayer.GetClipDuration();
					intPosition = this.DShowPlayer.GetCurrentPosition();
					result = true;
					return result;
				}
			}
			Duration = "";
			Position = "";
			result = false;
			return result;
		}
	}
}
